// ====================Sprite_Battler====================

SDS_CARD_COPY.Sprite_Enemy_loadBitmap = Sprite_Enemy.prototype.loadBitmap;
Sprite_Enemy.prototype.loadBitmap = function (name) {
    SDS_CARD_COPY.Sprite_Enemy_loadBitmap.call(this, name);
    if (CardGameManager.isCardGame()) {
        this.bitmap.addLoadListener(() => {
            this.createGaugeSprite();
        })
    }
};

SDS_CARD_COPY.Sprite_Enemy_updateBitmap = Sprite_Actor.prototype.updateBitmap;
Sprite_Actor.prototype.updateBitmap = function () {
    if (CardGameManager.isCardGame()) {
        Sprite_Battler.prototype.updateBitmap.call(this);
        const name = this._actor.battlerName();
        if (this._battlerName !== name) {
            this._battlerName = name;
            this._mainSprite.bitmap = ImageManager.loadSvActor(name);
            this.createGaugeSprite();
        }
    } else {
        SDS_CARD_COPY.Sprite_Enemy_updateBitmap.call(this);
    }
};

Sprite_Battler.prototype.battler = function () {
    return this._battler;
}

Sprite_Battler.prototype.createGaugeSprite = function () {
    if (!!this._gaugeSprite) {
        this._gaugeSprite.setObject(this._battler);
    }
    const width = this.constructor.name === "Sprite_Actor" ? 100 : this.bitmap?.width;
    const height = this.bitmap?.height;
    const opts = {
        width: width,
        height: 16,
        rim: 1,
        bgColour: "#cccaca",
        vColour: "#ff0000",
        tColour: "#00ff00"
    }
    if (!!this._gaugeSprite) return;
    const sprite = new CardHpGauge(opts);
    sprite.setObject(this._battler);
    sprite.x = -width / 2;
    sprite.y = 15;
    this.addChild(sprite);
    this._gaugeSprite = sprite;
    // 行动预告精灵
    if (this.constructor.name === "Sprite_Enemy") {
        this.createProphesySprite();
        this._prophesySprite.move(0, -height);
        this.refreshProphesy();
    }
}

/**
 * 创建行动预告精灵
 */
Sprite_Battler.prototype.createProphesySprite = function () {
    const bitmap = new Bitmap(150, 22);
    const sprite = new Sprite(bitmap);
    sprite.anchor.set(0.5, 1);
    this._prophesySprite = sprite;
    this.addChild(sprite);
}

Sprite_Battler.prototype.refreshProphesy = function () {
    if(!this._prophesySprite) return;
    let text = "";
    const bitmap = this._prophesySprite.bitmap;
    const action = this._enemy.currentAction();
    if (!!action) {
        const actionName = action.item()?.name;
        text = actionName ? `${actionName} 准备中`: "发呆中";
    }
    bitmap.clear();
    bitmap.drawText(text, 0, 0, bitmap.width, bitmap.height, "center");
}

Sprite_Battler.prototype.clearProphesy = function () {
    const bitmap = this._prophesySprite.bitmap;
    bitmap.clear();
}
